<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="盘点编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkNo">
              <a-input v-model="model.checkNo" placeholder="请输入盘点编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="盘点数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input v-model="total" placeholder="请输入盘点数量"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="组织" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="organization">
              <a-input v-model="model.organization" placeholder="请输入组织"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <a-input v-model="model.state * 1 == 0 ? `未完成` : `已完成`" placeholder="请输入状态"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="company">
              <a-input v-model="model.company" placeholder="请输入所属公司"></a-input>
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item label="所属仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehourse">
              <a-input v-model="model.warehourse" placeholder="请输入所属仓库"></a-input>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
    <a-input-search
      v-model="searchKey"
      placeholder="请输入规格搜索或批次号搜索"
      enter-button
      style="margin-bottom: 20px"
      allowClear
    />
    <a-table :columns="columns" :data-source="filterData" :scroll="{ y: 300 }" :pagination="false"> </a-table>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { queryCheckDetail } from '@/api/warehouse'
export default {
  name: 'WmsCheckForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/wms/wmsCheck/add',
        edit: '/wms/wmsCheck/edit',
        queryById: '/wms/wmsCheck/queryById',
      },
      searchKey: null,
      dataSource: [],
      columns: [
        {
          title: '规格',
          dataIndex: 'productSpec',
          key: 'productSpec',
          width: 120,
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          key: 'productName',
        },
        {
          title: '批次',
          dataIndex: 'batch',
          key: 'batch',
          width: 200,
        },
        {
          title: '数量',
          dataIndex: 'sums',
          key: 'sums',
          width: 120,
        },
      ],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    filterData() {
      const { dataSource, searchKey } = this
      if (searchKey) {
        return dataSource.filter((f) => f.productSpec.indexOf(searchKey) > -1 || f.batch.indexOf(searchKey) > -1)
      } else {
        return dataSource
      }
    },
    total() {
      let num = 0
      const { dataSource } = this
      dataSource.forEach((e) => {
        num += e.sums
      })
      return num
    },
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    async edit(record) {
      try {
        const params = { id: record.id }
        const r = await queryCheckDetail(params)
        this.model = r.result.wmsCheck
        this.dataSource = r.result.detailList
      } catch (_) {}
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>